import dataRdb from '@ohos.data.rdb'

export class Rdb {
  rdbStore: dataRdb.RdbStore = null
  private promiseExecSql: any = null
  private sqlCreateTable: string = ''
  private STORE_CONFIG = { name: 'my.db' }

  constructor(sqlCreateTable: string) {
    this.sqlCreateTable = sqlCreateTable
  }

  // 获取rdbStore
  async getRdbStore() {
    if (this.rdbStore != null) {
      return this.rdbStore.executeSql(this.sqlCreateTable);
    }
    let getPromiseRdb = dataRdb.getRdbStore(globalThis.context, this.STORE_CONFIG, 1);
    await getPromiseRdb.then(async (rdbStore) => {
      this.rdbStore = rdbStore;
      // 创建表
      this.promiseExecSql = rdbStore.executeSql(this.sqlCreateTable);
    }).catch((err) => {
      console.log("getRdbStore err." + JSON.stringify(err));
    });
  }
}